<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="推送目标app" label-width="100px" prop="appId">
        <el-input
          v-model="queryParams.appId"
          placeholder="请输入推给哪个app"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="目标用户组ID" label-width="100px" prop="targetGroupId">
        <el-input
          v-model="queryParams.targetGroupId"
          placeholder="请输入目标用户组ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="推送时间" label-width="100px" prop="pushTime">
        <el-date-picker clearable
          v-model="queryParams.pushTime"
          type="datetime"
          value-format="yyyy-MM-dd hh:mm:ss"
          placeholder="请选择推送时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="推送频率" label-width="100px" prop="pushFrequency">
        <el-input
          v-model="queryParams.pushFrequency"
          placeholder="请输入推送频率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="推送目标" label-width="100px" prop="pushTarget">
        <el-input
          v-model="queryParams.pushTarget"
          placeholder="请输入推送目标"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      
      <el-form-item label="创建人" label-width="100px" prop="creator">
        <el-input
          v-model="queryParams.creator"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新时间" label-width="100px" prop="lastUpdateTime">
        <el-date-picker clearable
          v-model="queryParams.lastUpdateTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新人" label-width="100px" prop="updater">
        <el-input
          v-model="queryParams.updater"
          placeholder="请输入更新人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="推送状态" label-width="100px" prop="pushStatus">
        <el-select v-model="queryParams.pushStatus" placeholder="请选择推送状态" clearable>
            <el-option
              v-for="dict in dict.type.sys_push_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:basicPushConfig:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:basicPushConfig:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:basicPushConfig:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:basicPushConfig:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="basicPushConfigList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="配置编号" align="center" prop="pushConfigId" />
      <el-table-column label="推送app" align="center" prop="appId" />
      <el-table-column label="推送内容" align="center" prop="pushContent" />
      <el-table-column label="推送时间" align="center" prop="pushTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.pushTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="推送状态" align="center" prop="pushStatus" >
        <template slot-scope="scope" >
          <dict-tag :options="dict.type.sys_push_status" :value="scope.row.pushStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="推送频率" align="center" prop="pushFrequency" />
      <el-table-column label="推送目标" align="center" prop="pushTarget" />
      <el-table-column label="用户ID" align="center" prop="targetUserIds" />
      <el-table-column label="用户组ID" align="center" prop="targetGroupId" />
      <el-table-column label="错误信息" align="center" prop="errorMessage" />
      <el-table-column label="创建人" align="center" prop="creator" />
      <el-table-column label="更新时间" align="center" prop="lastUpdateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastUpdateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updater" />
      <el-table-column label="操作"  fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:basicPushConfig:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:basicPushConfig:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改推送配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="推送目标app" label-width="100px" prop="appId">
          <el-input v-model="form.appId" placeholder="请输入推给哪个app" />
        </el-form-item>
        <el-form-item label="目标用户ID" label-width="100px" prop="targetUserIds">
          <el-input v-model="form.targetUserIds" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="目标用户组ID" label-width="100px" prop="targetGroupId">
          <el-input v-model="form.targetGroupId" placeholder="请输入目标用户组ID" />
        </el-form-item>
        <el-form-item label="推送时间" prop="pushTime">
          <el-date-picker clearable
            v-model="form.pushTime"
            type="datetime"
            value-format="yyyy-MM-dd hh:mm:ss"
            placeholder="请选择推送时间">
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="推送状态" prop="pushStatus">
          <el-select v-model="form.pushStatus" placeholder="请选择" clearable>
            <el-option
              v-for="dict in dict.type.sys_push_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item label="推送频率" prop="pushFrequency">
          <el-input v-model="form.pushFrequency" placeholder="请输入推送频率" />
        </el-form-item>
        <el-form-item label="推送目标" prop="pushTarget">
          <el-input v-model="form.pushTarget" placeholder="请输入推送目标" />
        </el-form-item>
        <el-form-item label="错误信息" prop="errorMessage">
          <el-input v-model="form.errorMessage" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="推送内容">
          <editor v-model="form.pushContent" :min-height="192"/>
        </el-form-item>
        <!-- <el-form-item label="创建人" prop="creator">
          <el-input v-model="form.creator" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="上次更新时间" prop="lastUpdateTime">
          <el-date-picker clearable
            v-model="form.lastUpdateTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择上次更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新人" prop="updater">
          <el-input v-model="form.updater" placeholder="请输入更新人" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBasicPushConfig, getBasicPushConfig, delBasicPushConfig, addBasicPushConfig, updateBasicPushConfig } from "@/api/system/basicPushConfig";

export default {
  name: "BasicPushConfig",
  dicts:['sys_push_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 推送配置表格数据
      basicPushConfigList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        appId: null,
        pushContent: null,
        pushTime: null,
        pushStatus: null,
        pushFrequency: null,
        pushTarget: null,
        targetUserIds: null,
        targetGroupId: null,
        errorMessage: null,
        creator: null,
        lastUpdateTime: null,
        updater: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询推送配置列表 */
    getList() {
      this.loading = true;
      listBasicPushConfig(this.queryParams).then(response => {
        this.basicPushConfigList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        pushConfigId: null,
        appId: null,
        pushContent: null,
        pushTime: null,
        pushStatus: null,
        pushFrequency: null,
        pushTarget: null,
        targetUserIds: null,
        targetGroupId: null,
        errorMessage: null,
        createTime: null,
        creator: null,
        lastUpdateTime: null,
        updater: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.pushConfigId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加推送配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const pushConfigId = row.pushConfigId || this.ids
      getBasicPushConfig(pushConfigId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改推送配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.pushConfigId != null) {
            updateBasicPushConfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBasicPushConfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const pushConfigIds = row.pushConfigId || this.ids;
      this.$modal.confirm('是否确认删除推送配置编号为"' + pushConfigIds + '"的数据项？').then(function() {
        return delBasicPushConfig(pushConfigIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/basicPushConfig/export', {
        ...this.queryParams
      }, `basicPushConfig_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
